package com.example.apple.myapplication625hap.model;

import com.example.apple.myapplication625hap.activity.LoginActivity;
import com.example.apple.myapplication625hap.bean.UserBean;
import com.example.apple.myapplication625hap.iface.LoginListener;
import com.example.apple.myapplication625hap.serive.LoginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Apple on 2017/5/25.
 */

public class LoginModel {
    Retrofit retrofit;
    private static String BASEURL="http://amicool.neusoft.edu.cn/";
    final static String PREFNAME="pref_login";
    public LoginModel() {
       retrofit=new Retrofit.Builder()
               .baseUrl(BASEURL)
               .addConverterFactory(GsonConverterFactory.create())
               .build();

    }

    public void login(String userName, String passWord, final LoginListener listener) {
        LoginService service=retrofit.create(LoginService.class);
        Call<UserBean> call=service.getLogin(userName,passWord);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if (response.body()!=null &&response.isSuccessful()){

                    listener.onResponse(response.body());
                }
                else {

                    listener.onFail("onResponse fail");
                }
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });

    }
}
